/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dp1.titandevelop.titano.view.prod;

import dp1.titandevelop.titano.bean.Estado;
import dp1.titandevelop.titano.bean.Mensajes;
import dp1.titandevelop.titano.persistent.Demanda;
import dp1.titandevelop.titano.persistent.OrdenProduccion;
import dp1.titandevelop.titano.service.AlmacenService;
import dp1.titandevelop.titano.service.DemandaService;
import dp1.titandevelop.titano.service.OrdenProduccionService;
import java.awt.Dimension;
import java.text.DateFormat;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;


/**
 *
 * @author chrestean
 */
public class ModulosT_PRO_OrdPro extends javax.swing.JInternalFrame {

       public List<OrdenProduccion> listaOrdPr = null;
       public ModulosT_PRO_OrdProDetal ventanaEdita;
       public Mensajes sms = new Mensajes();
       
       public void habilitaBotones() {
        this.btnEditar.setEnabled(true);
    }

    private void inhabilitaBotones() {
        this.btnEditar.setEnabled(false);
    }
    
    public void RefreshTable() {

        AbstractTableModel aux = (AbstractTableModel) this.tablaOrdPro.getModel();
        aux.fireTableDataChanged();
    }
       
   private AbstractTableModel ModeloTabla = new AbstractTableModel(){  
        //Atributos de los datos de la tabla
        private String [] titles = {"Demanda", "Fecha Demanda","Fecha Ord.Produccion","Estado"};
        
        @Override
        public int getRowCount() {
            return listaOrdPr.size();//tipos.size(); // cuantos registros (filas)
        }      
        
        @Override
        public int getColumnCount() {
            return 4; // cuantas columnas
        }          
        
        @Override
        public String getColumnName(int col){
            return titles[col];
        }                
       
        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            Object res=null;
            DateFormat fecha = DateFormat.getDateInstance(DateFormat.MEDIUM);
            OrdenProduccion op = listaOrdPr.get(rowIndex);
            
            switch(columnIndex){
                case 0: res =op.getToDemanda().getDescripcion(); break;
                case 1: res = fecha.format(op.getToDemanda().getFecha()); break;
                case 2: res = fecha.format(op.getFecha()); break;
                case 3: res = Estado.devuelveCadena(op.getEstado());
                
            }
            return res;
        } 


    };
    
    public ModulosT_PRO_OrdPro() {
        initComponents();
        OrdenProduccionService ops = new OrdenProduccionService();
        listaOrdPr=ops.buscarTodos();
        this.tablaOrdPro.setModel(ModeloTabla);
        RefreshTable();
        this.inhabilitaBotones();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaOrdPro = new javax.swing.JTable();
        botonBuscar = new javax.swing.JButton();
        botonNuevo = new javax.swing.JButton();
        botonLimpiar = new javax.swing.JButton();
        botonSalir = new javax.swing.JButton();
        btnEditar = new javax.swing.JButton();

        setResizable(true);
        setTitle("Administrar Ord. Producción");
        setMinimumSize(new java.awt.Dimension(590, 325));
        setPreferredSize(new java.awt.Dimension(590, 325));

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Resultado de Búsqueda"));
        jPanel2.setFont(new java.awt.Font("Tekton Pro", 0, 11)); // NOI18N

        tablaOrdPro.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Ord. Prod"
            }
        ));
        tablaOrdPro.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tablaOrdProMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tablaOrdPro);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 508, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 15, Short.MAX_VALUE))
        );

        botonBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/buscar.png"))); // NOI18N
        botonBuscar.setToolTipText("Buscar");
        botonBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonBuscarActionPerformed(evt);
            }
        });

        botonNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/nuevo.png"))); // NOI18N
        botonNuevo.setToolTipText("Nuevo");
        botonNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonNuevoActionPerformed(evt);
            }
        });

        botonLimpiar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/limpiar.png"))); // NOI18N
        botonLimpiar.setToolTipText("Limpiar");
        botonLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonLimpiarActionPerformed(evt);
            }
        });

        botonSalir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/retroceder.png"))); // NOI18N
        botonSalir.setToolTipText("Sali");
        botonSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonSalirActionPerformed(evt);
            }
        });

        btnEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/detalle.png"))); // NOI18N
        btnEditar.setEnabled(false);
        btnEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnEditar)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(botonBuscar)
                            .addGap(0, 0, 0)
                            .addComponent(botonNuevo)
                            .addGap(0, 0, 0)
                            .addComponent(botonLimpiar)
                            .addGap(0, 0, 0)
                            .addComponent(botonSalir))
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(24, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonBuscar)
                    .addComponent(botonNuevo)
                    .addComponent(botonLimpiar)
                    .addComponent(botonSalir))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnEditar)
                .addContainerGap(21, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botonNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonNuevoActionPerformed
        try{ 
            AlmacenService sa = new AlmacenService();
            if(!sa.buscar().isEmpty()){
                ModulosT_PRO_OrdProNuevo ordpronuevo = new ModulosT_PRO_OrdProNuevo();
                this.getParent().add(ordpronuevo);
                Dimension desktopSize = this.getParent().getSize();
                Dimension jInternalFrameSize = ordpronuevo.getSize();
                ordpronuevo.setLocation((desktopSize.width - jInternalFrameSize.width) / 2,
                        (desktopSize.height - jInternalFrameSize.height) / 4);
                ordpronuevo.show();
                this.dispose();
            }
            else{
                JOptionPane.showInternalMessageDialog(this.getRootPane(),"No se puede crear una orden de producción sin tener almacenes","Aviso",JOptionPane.WARNING_MESSAGE);
            }
        }catch(Exception e){
            JOptionPane.showInternalMessageDialog(this.getRootPane(),sms.getError(),"Error",JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_botonNuevoActionPerformed

    private void botonLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonLimpiarActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_botonLimpiarActionPerformed

    private void botonSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonSalirActionPerformed
    this.dispose();        // TODO add your handling code here:
    }//GEN-LAST:event_botonSalirActionPerformed

    private void botonBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonBuscarActionPerformed
    }//GEN-LAST:event_botonBuscarActionPerformed

    private void btnEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarActionPerformed
        
        OrdenProduccion op;
        op = listaOrdPr.get(this.tablaOrdPro.getSelectedRow());
        
        ventanaEdita = new ModulosT_PRO_OrdProDetal(op);
        
        this.getParent().add(ventanaEdita);
        Dimension desktopSize = this.getParent().getSize();
        Dimension jInternalFrameSize = ventanaEdita.getSize();
        ventanaEdita.setLocation((desktopSize.width - jInternalFrameSize.width) / 2, (desktopSize.height - jInternalFrameSize.height) / 4);
        ventanaEdita.show();
        
    }//GEN-LAST:event_btnEditarActionPerformed

    private void tablaOrdProMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaOrdProMouseClicked
      if(evt.getClickCount()==1){
            this.habilitaBotones();
        }
    }//GEN-LAST:event_tablaOrdProMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonBuscar;
    private javax.swing.JButton botonLimpiar;
    private javax.swing.JButton botonNuevo;
    private javax.swing.JButton botonSalir;
    private javax.swing.JButton btnEditar;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tablaOrdPro;
    // End of variables declaration//GEN-END:variables

    private void setLocationRelativeTo(Object object) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    private Object getparent() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}
